package snmpstatistics;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import snmpstatistics.StatsToShow.Statistics;

import features.SNMPClient;
import features.SNMPException;
import features.SNMPSession;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class StatsPanel extends JPanel {
	private static final long serialVersionUID = -2680646872326417418L;

	private static final long POLLING_TIME = 1000;
	private static final long POLLING_TIME_PACKETS_STATS = 10000;
	private Timer timer = null;
	
	private JPanel pnlGraph = null;
	private JPanel pnlGraphContainer;
	private JLabel lblBadValues;
	private JLabel lblGenErrs;
	private JLabel lblGetNexts;
	private JLabel lblTraps;
	private JLabel lblTooBigs;
	private JLabel lblSetRequests;
	private JLabel lblPackets;
	private JLabel lblNoSuchNames;
	private JLabel lblGetResponses;
	private JLabel lblGetRequests;
	private JLabel lblOutPDUErrorRate;
	private JLabel lblInPDUErrorRate;
	private JLabel txtGenErrs;
	private JLabel txtTraps;
	private JLabel lblOutTotalPkts;
	private JLabel lblInTotalPkts;
	private JLabel txtBadValues;
	private JLabel txtGetRequests;
	private JLabel txtGetResponses;
	private JLabel txtSetRequests;
	private JLabel txtTooBigs;
	private JLabel txtPackets;
	private JLabel txtNoSuchNames;
	private JLabel txtGetNexts;
	private JLabel txtInErrPkts;
	private JLabel txtOutErrPkts;
	private JLabel txtTotalInPkts;
	private JLabel txtTotalOutPkts;

	private StatsToShow statsToShow;

	/**
	 * This is the default constructor
	 */
	public StatsPanel(SNMPSession session, StatsToShow statisticsToShow) {
		super();
		this.statsToShow = statisticsToShow;
		initialize();
		
		timer = new Timer();
		TimerTask task = new RetrieveStats(session, this);
		if(statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS) || statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS)){
			timer.schedule(task, 0, POLLING_TIME_PACKETS_STATS);
		}
		else
			timer.schedule(task, POLLING_TIME, POLLING_TIME);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 487);
		GridBagLayout thisLayout = new GridBagLayout();
		this.setLayout(thisLayout);
		thisLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
		thisLayout.columnWidths = new int[] { 130, 110, 40, 130, 110 };
		thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1};
		thisLayout.rowHeights = new int[] {20, 20, 20, 20, 20, 20, 20, 7};
		this.setPreferredSize(new java.awt.Dimension(600, 487));
		
		boolean disableOtherStatistics = statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS) || statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS);
		
		lblInPDUErrorRate = new JLabel("In PDU error rate: ");
		this.add(lblInPDUErrorRate, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblInPDUErrorRate.setEnabled(statsToShow.isEnabled(Statistics.IN_PDU_ERROR_RATE) && !disableOtherStatistics);
		this.add(getTxtInPDUErrorRate(statsToShow.isEnabled(Statistics.IN_PDU_ERROR_RATE) && !disableOtherStatistics), new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblOutPDUErrorRate = new JLabel("Out PDU error rate: ");
		this.add(lblOutPDUErrorRate, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblOutPDUErrorRate.setEnabled(statsToShow.isEnabled(Statistics.OUT_PDU_ERROR_RATE) && !disableOtherStatistics);
		this.add(getTxtOutPDUErrorRate(statsToShow.isEnabled(Statistics.OUT_PDU_ERROR_RATE) && !disableOtherStatistics), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		
		lblBadValues = new JLabel("Bad values: ");
		this.add(lblBadValues, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblBadValues.setEnabled(statsToShow.isEnabled(Statistics.BAD_VALUES) && !disableOtherStatistics);
		this.add(getTxtBadValues(statsToShow.isEnabled(Statistics.BAD_VALUES) && !disableOtherStatistics), new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblGenErrs = new JLabel("Gen errs: ");
		this.add(lblGenErrs, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblGenErrs.setEnabled(statsToShow.isEnabled(Statistics.GEN_ERR) && !disableOtherStatistics);
		this.add(getTxtGenErrs(statsToShow.isEnabled(Statistics.GEN_ERR) && !disableOtherStatistics), new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblGetNexts = new JLabel("Get nexts: ");
		this.add(lblGetNexts, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblGetNexts.setEnabled(statsToShow.isEnabled(Statistics.GET_NEXT) && !disableOtherStatistics);
		this.add(getTxtGetNexts(statsToShow.isEnabled(Statistics.GET_NEXT) && !disableOtherStatistics), new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblGetRequests = new JLabel("Get requests: ");
		this.add(lblGetRequests, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblGetRequests.setEnabled(statsToShow.isEnabled(Statistics.GET_REQUEST) && !disableOtherStatistics);
		this.add(getTxtGetRequests(statsToShow.isEnabled(Statistics.GET_REQUEST) && !disableOtherStatistics), new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblGetResponses = new JLabel("Get responses: ");
		this.add(lblGetResponses, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblGetResponses.setEnabled(statsToShow.isEnabled(Statistics.GET_RESPONSE) && !disableOtherStatistics);
		this.add(getTxtGetResponses(statsToShow.isEnabled(Statistics.GET_RESPONSE) && !disableOtherStatistics), new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblNoSuchNames = new JLabel("No such names: ");
		this.add(lblNoSuchNames, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblNoSuchNames.setEnabled(statsToShow.isEnabled(Statistics.NO_SUCH_NAME) && !disableOtherStatistics);
		this.add(getTxtNoSuchNames(statsToShow.isEnabled(Statistics.NO_SUCH_NAME) && !disableOtherStatistics), new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblPackets = new JLabel("Pacchetti: ");
		this.add(lblPackets, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblPackets.setEnabled(statsToShow.isEnabled(Statistics.PACKETS) && !disableOtherStatistics);
		this.add(getTxtPackets(statsToShow.isEnabled(Statistics.PACKETS) && !disableOtherStatistics), new GridBagConstraints(4, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblSetRequests = new JLabel("Set requests: ");
		this.add(lblSetRequests, new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblSetRequests.setEnabled(statsToShow.isEnabled(Statistics.SET_REQUEST) && !disableOtherStatistics);
		this.add(getTxtSetRequests(statsToShow.isEnabled(Statistics.SET_REQUEST) && !disableOtherStatistics), new GridBagConstraints(4, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblTooBigs = new JLabel("Too bigs: ");
		this.add(lblTooBigs, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblTooBigs.setEnabled(statsToShow.isEnabled(Statistics.TOO_BIG) && !disableOtherStatistics);
		this.add(getTxtTooBigs(statsToShow.isEnabled(Statistics.TOO_BIG) && !disableOtherStatistics), new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		lblTraps = new JLabel("Traps: ");
		this.add(lblTraps, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		lblTraps.setEnabled(statsToShow.isEnabled(Statistics.TRAPS) && !disableOtherStatistics);
		this.add(getTxtTraps(statsToShow.isEnabled(Statistics.TRAPS) && !disableOtherStatistics), new GridBagConstraints(4, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

		this.add(getLblInTotalPkts(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		lblInTotalPkts.setEnabled(statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS));
		this.add(getTxtTotalInPkts(statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS) && !disableOtherStatistics), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		
		this.add(getLblOutTotalPkts(), new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		lblOutTotalPkts.setEnabled(statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS));
		this.add(getTxtTotalOutPkts(statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS) && !disableOtherStatistics), new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		
		this.add(getPnlGraphContainer(), new GridBagConstraints(0, 7, 5, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
	}
	
	private JLabel getTxtTotalInPkts(boolean enabled) {
		if (txtTotalInPkts == null) {
			txtTotalInPkts = new JLabel();
			txtTotalInPkts.setText("-");
		}
		txtTotalInPkts.setEnabled(enabled);
		return txtTotalInPkts;
	}

	private JLabel getTxtTotalOutPkts(boolean enabled) {
		if (txtTotalOutPkts == null) {
			txtTotalOutPkts = new JLabel();
			txtTotalOutPkts.setText("-");
		}
		txtTotalOutPkts.setEnabled(enabled);
		return txtTotalOutPkts;
	}

	private JLabel getTxtGetNexts(boolean enabled) {
		if (txtGetNexts == null) {
			txtGetNexts = new JLabel();
			txtGetNexts.setText("-/-");
		}
		txtGetNexts.setEnabled(enabled);
		return txtGetNexts;
	}

	private JLabel getTxtGenErrs(boolean enabled) {
		if (txtGenErrs == null) {
			txtGenErrs = new JLabel();
			txtGenErrs.setText("-/-");
		}
		txtGenErrs.setEnabled(enabled);
		return txtGenErrs;
	}

	private JLabel getTxtBadValues(boolean enabled) {
		if (txtBadValues == null) {
			txtBadValues = new JLabel();
			txtBadValues.setText("-/-");
		}
		txtBadValues.setEnabled(enabled);
		return txtBadValues;
	}

	private JLabel getTxtGetResponses(boolean enabled) {
		if (txtGetResponses == null) {
			txtGetResponses = new JLabel();
			txtGetResponses.setText("-/-");
		}
		txtGetResponses.setEnabled(enabled);
		return txtGetResponses;
	}

	private JLabel getTxtTraps(boolean enabled) {
		if (txtTraps == null) {
			txtTraps = new JLabel();
			txtTraps.setText("-/-");
		}
		txtTraps.setEnabled(enabled);
		return txtTraps;
	}

	private JLabel getTxtTooBigs(boolean enabled) {
		if (txtTooBigs == null) {
			txtTooBigs = new JLabel();
			txtTooBigs.setText("-/-");
		}
		txtTooBigs.setEnabled(enabled);
		return txtTooBigs;
	}

	private JLabel getTxtSetRequests(boolean enabled) {
		if (txtSetRequests == null) {
			txtSetRequests = new JLabel();
			txtSetRequests.setText("-/-");
		}
		txtSetRequests.setEnabled(enabled);
		return txtSetRequests;
	}

	private JLabel getTxtGetRequests(boolean enabled) {
		if (txtGetRequests == null) {
			txtGetRequests = new JLabel();
			txtGetRequests.setText("-/-");
		}
		txtGetRequests.setEnabled(enabled);
		return txtGetRequests;
	}

	private JLabel getTxtPackets(boolean enabled) {
		if (txtPackets == null) {
			txtPackets = new JLabel();
			txtPackets.setText("-/-");
		}
		txtPackets.setEnabled(enabled);
		return txtPackets;
	}

	private JLabel getTxtNoSuchNames(boolean enabled) {
		if (txtNoSuchNames == null) {
			txtNoSuchNames = new JLabel();
			txtNoSuchNames.setText("-/-");
		}
		txtNoSuchNames.setEnabled(enabled);
		return txtNoSuchNames;
	}

	private JLabel getTxtInPDUErrorRate(boolean enabled) {
		if (txtInErrPkts == null) {
			txtInErrPkts = new JLabel();
			txtInErrPkts.setText("-/-");
		}
		txtInErrPkts.setEnabled(enabled);
		return txtInErrPkts;
	}

	private JLabel getTxtOutPDUErrorRate(boolean enabled) {
		if (txtOutErrPkts == null) {
			txtOutErrPkts = new JLabel();
			txtOutErrPkts.setText("-/-");
		}
		txtOutErrPkts.setEnabled(enabled);
		return txtOutErrPkts;
	}

	private JPanel getPnlGraphContainer() {
		if (pnlGraphContainer == null) {
			pnlGraphContainer = new JPanel();
			pnlGraphContainer.setBorder(BorderFactory
					.createTitledBorder("Cronologia"));
			pnlGraphContainer.setPreferredSize(new java.awt.Dimension(540, 270));
			pnlGraphContainer.setSize(503, 270);
			pnlGraphContainer.setLayout(null);
			pnlGraphContainer.add(getPnlGraph());

		}
		return pnlGraphContainer;
	}

	private JPanel getPnlGraph() {
		if (pnlGraph == null) {
			if(statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS) || statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS))
				pnlGraph = new Graph(statsToShow, POLLING_TIME_PACKETS_STATS);
			else
				pnlGraph = new Graph(statsToShow, POLLING_TIME);
			pnlGraph.setBounds(13, 14, 510, 280);
		}

		return pnlGraph;
	}

	public void updateStats(SNMPStats stats) {
		((Graph) getPnlGraph()).addStats(stats);
		
		getTxtGenErrs(statsToShow.isEnabled(Statistics.GEN_ERR)).setText(stats.getInGenErrs() + "/" + stats.getOutGenErrs());
		getTxtTraps(statsToShow.isEnabled(Statistics.TRAPS)).setText(stats.getInTraps() + "/" + stats.getOutTraps());
		getTxtBadValues(statsToShow.isEnabled(Statistics.BAD_VALUES)).setText(stats.getInBadValues() + "/" + stats.getOutBadValues());
		getTxtGetRequests(statsToShow.isEnabled(Statistics.GET_REQUEST)).setText(stats.getInGetRequests() + "/" + stats.getOutGetRequests());
		getTxtGetResponses(statsToShow.isEnabled(Statistics.GET_RESPONSE)).setText(stats.getInGetResponses() + "/" + stats.getOutGetResponses());
		getTxtSetRequests(statsToShow.isEnabled(Statistics.SET_REQUEST)).setText(stats.getInSetRequests() + "/" + stats.getOutSetRequests());
		getTxtTooBigs(statsToShow.isEnabled(Statistics.TOO_BIG)).setText(stats.getInTooBigs() + "/" + stats.getOutTooBigs());
		getTxtPackets(statsToShow.isEnabled(Statistics.PACKETS)).setText(stats.getInPkts() + "/" + stats.getOutPkts());
		getTxtNoSuchNames(statsToShow.isEnabled(Statistics.NO_SUCH_NAME)).setText(stats.getInNoSuchNames() + "/" + stats.getOutNoSuchNames());
		getTxtGetNexts(statsToShow.isEnabled(Statistics.GET_NEXT)).setText(stats.getInGetNexts() + "/" + stats.getOutGetNexts());
		getTxtInPDUErrorRate(statsToShow.isEnabled(Statistics.IN_PDU_ERROR_RATE)).setText(stats.getInErrPkts() + "/" + stats.getInPkts());
		getTxtOutPDUErrorRate(statsToShow.isEnabled(Statistics.OUT_PDU_ERROR_RATE)).setText(stats.getOutErrPkts() + "/" + stats.getOutPkts());
		getTxtTotalInPkts(statsToShow.isEnabled(Statistics.TOTAL_IN_PACKETS)).setText(""+stats.getTotalInPkts());
		getTxtTotalOutPkts(statsToShow.isEnabled(Statistics.TOTAL_OUT_PACKETS)).setText(""+stats.getTotalOutPkts());
	}
	
	public static void main(String[] args) throws SNMPException {
		SNMPSession session = SNMPClient.startSession(SNMPClient.VERSION_2C);
		session.init(SNMPClient.DEFAULT_IP, SNMPClient.DEFAULT_PORT_STRING, SNMPClient.DEFAULT_COMMUNITY);
		
		JFrame frame = new JFrame();
		frame.setLayout(null);
		
		StatsPanel thisClass = new StatsPanel(session, new StatsToShow());
		frame.add(thisClass);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(620, 430);
		frame.setVisible(true);
	}
	
	private JLabel getLblInTotalPkts() {
		if(lblInTotalPkts == null) {
			lblInTotalPkts = new JLabel();
			lblInTotalPkts.setText("In total packets: ");
		}
		return lblInTotalPkts;
	}
	
	private JLabel getLblOutTotalPkts() {
		if(lblOutTotalPkts == null) {
			lblOutTotalPkts = new JLabel();
			lblOutTotalPkts.setText("Out total packets:");
		}
		return lblOutTotalPkts;
	}
}
